package com.northpool.operator.statistics.dataset.math;

import com.northpool.operator.common.database.DatabaseUtils;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.utils.StpOperatorHelper;
import com.northpool.resources.datasource.IDataSource;
import com.northpool.resources.datatable.operate.ITableOperator;
public abstract class BaseMathStatistical extends StpOperatorHelper {

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        getValueInfo(paramTool);
        try {
            this.log("统计表数据信息...");
//            this.progress(0);
            executeStatistical(paramTool);
//            this.progress(100);
        } finally {
            if (datasource != null) {
                datasource.destroy();
            }
        }
    }

    protected String dataSourceId;
    protected String tableName;
    protected IDataSource datasource=null;
    protected ITableOperator tableOperator=null;

    private void getValueInfo(ParamTool paramTool) throws Exception{
        dataSourceId = paramTool.getStr("dataSourceId");
        tableName = paramTool.getStr("tableName");
        datasource = IDataSource.fromJSON(DatabaseUtils.getDataSourceJson(dataSourceId));
        tableOperator = datasource.getTableOperator(tableName);
    }

    protected abstract void executeStatistical(ParamTool paramTool);

    protected void writeResult(Object result) {
        this.result("statisticalResults", result.toString());
    }
}
